﻿namespace AppShare
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.ServiceModel.DomainServices.Client;
    using System.Windows;
    using System.Windows.Browser;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Input;
    using System.Windows.Navigation;
    using AppShare.Data.Models;
    using AppShare.Web;

    /// <summary>
    /// Home page for the application.
    /// </summary>
    public partial class Home : Page
    {
        private IList<OperationBase> possiblyPendingOperations = new List<OperationBase>();
        private ShareContext share = new ShareContext();
        private OperationHandler handler = new OperationHandler();


        /// <summary>
        /// Creates a new <see cref="Home"/> instance.
        /// </summary>
        public Home()
        {
            InitializeComponent();

            this.Title = ApplicationStrings.HomePageTitle;
        }

        /// <summary>
        /// Executes when the user navigates to this page.
        /// </summary>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }


        // Executes when the user tries to leave this page.
        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            foreach (OperationBase operation in this.possiblyPendingOperations)
            {
                if (!operation.IsComplete)
                {
                    if (operation.CanCancel)
                    {
                        operation.Cancel();
                    }
                    else
                    {
                        e.Cancel = true;
                    }
                }
            }
        }

        private void Accordion_Loaded(object sender, RoutedEventArgs e)
        {
            var acc = sender as FrameworkElement;

            var query = share.GetProductsQuery();
            var op = share.Load(query);
            possiblyPendingOperations.Add(op);
            handler.CurrentOperation = op;

            acc.DataContext = op.Entities;
        }


        private void BusyIndicator_Loaded(object sender, RoutedEventArgs e)
        {
            var indicator = sender as FrameworkElement;
            indicator.DataContext = handler;
        }

        private void Accordion_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 0)
                return;

            var product = e.AddedItems[0] as ProductData;

            if (product.Families == null)
            {
                var query = share.GetFamiliesQuery(product.ProductId);
                var op = share.Load(query);
                possiblyPendingOperations.Add(op);
                handler.CurrentOperation = op;

                product.Families = op.Entities;
            }
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 0)
                return;

            var family = e.AddedItems[0] as FamilyData;

            var query = share.GetShipsQuery(family.FamilyId);
            var op = share.Load(query);
            possiblyPendingOperations.Add(op);
            handler.CurrentOperation = op;

            dataPanel.DataContext = new PagedCollectionView(op.Entities);
        }

        private void ContentText_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.Current.IsRunningOutOfBrowser)
            {
                var ui = sender as FrameworkElement;
                try
                {
                    ui.DataContext = HtmlPage.BrowserInformation.ProductName;
                    ui.Visibility = Visibility.Collapsed;
                }
                catch (Exception ex)
                {
                    ui.DataContext = "您的浏览器不能完全支持Silverlight，信息：" + ex.Message;
                }
            }
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            CopyToClipboard(sender);
        }

        private void Details_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            CopyToClipboard(sender);
            e.Handled = true;
        }

        private void CopyToClipboard(object sender)
        {
            var ui = sender as FrameworkElement;
            var ship = ui.DataContext as ShipData;
            if (ship.IsAvailable)
            {
                Clipboard.SetText(ship.ED2K);
                MessageBox.Show("文件连接已经复制到您的剪贴板中",
                    ApplicationStrings.ApplicationName, MessageBoxButton.OK);
            }
        }

    }
}